package com.waya.web.browser.js_interface;

import android.util.ArrayMap;
import android.webkit.WebView;
import java.util.Map;
import java.util.Set;

/**
 * Created by JesseHev on 2017/12/5.
 * describe:
 */

public class JSInterfaceImp extends AbsIJSInterface {

  private WebView mWebView;

  public JSInterfaceImp(WebView webView) {
    mWebView = webView;
  }

  @Override public IJSInterface addJavaObjects(Map<String, Object> maps) {

    Set<Map.Entry<String, Object>> sets = maps.entrySet();
    for (Map.Entry<String, Object> entry : sets) {
      Object obj = entry.getValue();
      if (!checkObject(obj)) {
        throw new RuntimeException("this obj has not offer js method to be called!");
      }
      performAddJavaObject(entry.getKey(), obj);
    }

    return null;
  }

  @Override public IJSInterface addJavaObject(String name, Object obj) {
    if (!checkObject(obj)) {
      throw new RuntimeException("this obj has not offer js method to be called!");
    }
    performAddJavaObject(name, obj);
    return this;
  }


  private IJSInterface performAddJavaObject(String name, Object obj) {
    mWebView.addJavascriptInterface(obj, name);
    return this;
  }
}
